﻿/*
 Copyright (c) 2009 Alvin Cho (alvincho@risklib.com)
 Copyright (c) 2009 RiskLib.com
 
 This file is part of RiskLib.NET Project. See http://www.risklib.net for more information.
 
 RiskLib.NET is an open source project. Using of RiskLib.NET is free of charge.
 It is distributed under GNU General Public License version 2 (GPLv2).
 GPLv2 can be found at http://www.risklib.net/risklib/license.
 
 This program is distributed in the hope that it will be useful, but WITHOUT
 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 FOR A PARTICULAR PURPOSE.  See the license for more details.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RiskLib.DataSource
{
    public abstract class WSJUSStock : DailyClosingTextDataSource
    {
        public WSJUSStock(ExchangeCodeEnum exchangeCode, string dailyClosingUrl):base("WSJ")
        {
            url = new Uri( dailyClosingUrl);
            exchange = exchangeCode;
        }
        string wsjCookies = "survey=true; expires=" + DateTime.Now.AddDays(7).ToString("r") + "; path=/; domain=.wsj.com";
        protected Uri url;
        Regex reWSJ = new Regex("<tr([^>]*>){3}(?<EnglishName>[^<]+)([^>]+>){5}(?<Code>[^<]+)([^>]+>){5}(?<OpenPrice>[^<]+)([^>]+>){4}(?<HighPrice>[^<]+)([^>]+>){4}(?<LowPrice>[^>]+)([^>]+>){4}(?<ClosePrice>[\\d,.]+)([^>]+>){16}(?<Volume>[^<]+)", RegexOptions.IgnoreCase);
        Regex reDate = new Regex("<span class=(\")?p12(\")?>[^,]+day,\\s(?<date>[^<]+)([^>]*>){3}", RegexOptions.IgnoreCase);
        protected ExchangeCodeEnum exchange;
        /// <summary>
        /// Get daily closing data.
        /// </summary>
        /// <param name="date">This parameter is not used since WSJ provide only data of the last trading date.</param>
        /// <returns>DailyClosingList</returns>
        public override DailyClosingList GetDailyClosing(DateTime date)
        {
            string content= GetWebPage(url.AbsoluteUri);
            DateTime dataDate =DateTime.Parse( reDate.Match(content).Groups["date"].Value);

            return ParseDailyQuoteList(dataDate, content, reWSJ, HistoricalFields);
        }

        protected override IProductCode GetProductCodeFromMatch(Match match)
        {
            string code = match.Groups["Code"].Value;
            SimpleProduct product = new SimpleProduct(code);
            return product;
        }
        protected FieldMappingList historicalFields = new FieldMappingList()
        {
            {FieldNameEnum.OpenPrice,"OpenPrice"},
            {FieldNameEnum.HighPrice,"HighPrice"},
            {FieldNameEnum.LowPrice,"LowPrice"},
            {FieldNameEnum.ClosePrice,"ClosePrice"},
            {FieldNameEnum.Volume,"Volume"}
        };
        public override FieldMappingList HistoricalFields
        {
            get
            {
                return historicalFields;
            }
            set
            {
                historicalFields = value;
            }
        }
    }
    public class WSJNYSEStockDataSource : WSJUSStock
    {
        public WSJNYSEStockDataSource()
            : base(ExchangeCodeEnum.XNYS, "http://online.wsj.com/mdc/public/page/2_3024-NYSE.html?mod=topnav_2_3021")
        { }
    }
}
